﻿using MobileBSC_Data;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileBSC_SQLite
{
    public class SignalAssociationMapReader
    {
        private WeakSignalReader SignalReader { get; set; }
        private KPIReader KPIReader { get; set; }

        private const string SELECT_STMT =
            "SELECT " +
                 "SignalAssociationMapID " +
               ", WeakSignalID " +
               ", KPIID " +
               ", Influence " +
            "FROM SignalAssociationMap ";

        public SignalAssociationMapReader()
        {
            SignalReader = new WeakSignalReader();
            KPIReader = new KPIReader();
        }
        public SignalAssociationMap ReadByID(long id)
        {
            var statement = SELECT_STMT + "WHERE SignalAssociationMapID = @id;";
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@id", id));

            return DbTool<SignalAssociationMap>.ReadSingle(statement, parameters, CreateSAM);
        }
        public List<SignalAssociationMap> ReadBySignal(long signalId)
        {
            var statement = SELECT_STMT + "WHERE WeakSignalID = @id;";
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@id", signalId));

            return DbTool<SignalAssociationMap>.ReadMultiple(statement, parameters, CreateSAM);
        }
        public List<SignalAssociationMap> ReadByKPI(long kpiId)
        {
            var statement = SELECT_STMT + "WHERE KPIID = @id;";
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@id", kpiId));

            return DbTool<SignalAssociationMap>.ReadMultiple(statement, parameters, CreateSAM);
        }
        private SignalAssociationMap CreateSAM(SQLiteDataReader rd)
        {
            var result = new SignalAssociationMap();
            result.SignalAssociationMapID = (long)rd["SignalAssociationMapID"];
            result.WeakSignal = SignalReader.ReadByID((long)rd["WeakSignalID"]);
            result.KPI = KPIReader.ReadByID((long)rd["KPIID"]);
            result.Influence = (decimal)rd["Influence"];

            return result;
        }

    }
}
